function threshold=autogetthreshold(structure)

% threshold=autogetthreshold(structure)
%
% Computes automatically the threshold level for given MAXISTRUCT.
%
% 2012 Ari-Pekka Honkanen

[thres area]=maskarea(structure); %Computes the area of the data pixels over threshold as a function of threshold
d2area=diff(diff(smooth(area,5))./diff(thres))./diff(thres(1:end-1)); %The second derivative of area

ind=round(2.5*find(d2area==max(d2area))); %The point where falling of the area as function of threshold suddenly ceases (the constant is somewhat arbitrary)

threshold=thres(ind);

end
